package com.timoon.webcomponent.utility.product.sort.sortnav1;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.sample.dao.model.ProductSort;

/**
 *<p>仿http://www.skcoo.cn/，商品分类,两级</p>
 *<p>public</p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class InsideNavsPanel extends Panel {

	@SpringBean(name="com.timoon.webcomponent.utility.navs.service.NavsService")
	private InsideNavsService service;
	
	private Class<?> targetPage;
	
	public InsideNavsPanel(String id,Class<?> targetPage) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.targetPage=targetPage;
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderImgHead();
		
		renderList();
	}

	private void renderList() {
		// TODO Auto-generated method stub
		
		List<ProductSort> parents=this.service.getParentProductSort();
		
		ListView<ProductSort> listView=new ListView<ProductSort>("wk_list",parents) {
			
			@Override
			protected void populateItem(ListItem<ProductSort> arg0) {
				// TODO Auto-generated method stub
				ProductSort parent=arg0.getModelObject();
				
				arg0.add(new Label("wk_pName",parent.getName()));
				
				GridView<ProductSort> gridView=renderGridView(parent);
				arg0.add(gridView);
				
				gridView.setColumns(2);
			}
		};
		
		add(listView);
	}

	protected GridView<ProductSort> renderGridView(ProductSort parent) {
		// TODO Auto-generated method stub
		
		List<ProductSort> childs=this.service.getChildProductSortByParentId(parent.getId());
		
		IDataProvider<ProductSort> data=new ListDataProvider<ProductSort>(childs);
		GridView<ProductSort> gridView=new GridView<ProductSort>("rows",data) {

			@Override
			protected void populateEmptyItem(Item<ProductSort> arg0) {
				// TODO Auto-generated method stub
				BookmarkablePageLink link=new BookmarkablePageLink("wk_link", targetPage);
				arg0.add(link);
				
				link.add(new Label("wk_label",""));	

			}

			@Override
			protected void populateItem(Item<ProductSort> arg0) {
				// TODO Auto-generated method stub
				
				ProductSort child=arg0.getModelObject();
				
				PageParameters parameter=new PageParameters();
				parameter.set("product_sort_id", child.getId());
				BookmarkablePageLink link=new BookmarkablePageLink("wk_link", targetPage,parameter);
				arg0.add(link);
				
				link.add(new Label("wk_label",child.getName()));
			}
		};
		return gridView;
	}

	private void renderImgHead() {
		// TODO Auto-generated method stub
		add(new Image("wk_head",new PackageResourceReference(this.getClass(),"head.png")));
	}

	public InsideNavsService getService() {
		return service;
	}

	public void setService(InsideNavsService service) {
		this.service = service;
	}

}
